package by.bucha.testproject.adapters;

import java.util.List;

import by.bucha.tabnavigationproject.R;
import by.bucha.testproject.xml.bo.QuoteModel;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ParsedXmlListAdapter extends ArrayAdapter<QuoteModel> {

	private int mTextViewResourceId;
	private List<QuoteModel> mItems;

	public ParsedXmlListAdapter(Context context, int textViewResourceId,
			List<QuoteModel> objects) {
		super(context, textViewResourceId, objects);
		mTextViewResourceId = textViewResourceId;
		mItems = objects;
	}

	@Override
	public QuoteModel getItem(int i) {
		if (mItems != null) {
			return mItems.get(i);
		} else {
			return null;
		}
	}

	@Override
	public int getCount() {
		if (mItems != null) {
			return mItems.size();
		} else {
			return 0;
		}
	}

	public void changeData(List<QuoteModel> items) {
		if (mItems != null) {
			mItems.clear();
		}
		mItems = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(mTextViewResourceId, null);
		}

		final QuoteModel model = mItems.get(position);
		if (model != null) {
			TextView id = (TextView) view
					.findViewById(R.id.view_fragment_parsed_xml_list_id_text);
			TextView date = (TextView) view
					.findViewById(R.id.view_fragment_parsed_xml_list_date_text);
			TextView text = (TextView) view
					.findViewById(R.id.view_fragment_parsed_xml_list_text);

			id.setText(model.getId());
			date.setText(model.getDate());
			text.setText(model.getText());

		}
		return view;
	}
}
